#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/11/10 9:41
# @Author  : ystraw
# @Site    : 
# @File    : dealWord.py
# @Software: PyCharm Community Edition
# @function: 将单词整理成规范格式

# 读取单词文件
def getWordStr(filename):
    with open(filename, 'r', encoding='utf-8') as f:
        wordstr = f.read()
    return wordstr

# 写入单词文件
def writeWordStr(filename, words):
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(words)
    print(filename + '写入成功！')

# 初步单词格式整理成文件一
def dealWord(wordstr):
    length = len(wordstr)
    i = 0
    while i < length - 2:
        # if '\u4e00' <= wordstr[i] <= '\u9fff' and (not ('\u4e00' <= wordstr[i+1] <= '\u9fff' or
        #    wordstr[i+1] == ',' or wordstr[i+1] == ';' or wordstr[i+1] == '；'or wordstr[i+1] == '('or wordstr[i+1] == ')')):
        #     wordstr = wordstr[:i+1] + '\n' + wordstr[i+2:]
        # i += 1
        if '\u4e00' <= wordstr[i] <= '\u9fff':
            flag = 0
            for j in range(i + 1, length):
                if wordstr[j] == ' ' or wordstr[j] == '\n':
                    pass
                if (wordstr[j] >= 'a' and wordstr[j] <= 'z') or (wordstr[j] >= 'A' and wordstr[j] <= 'Z'):
                    flag = 1
                    break
            if flag == 1:
                wordstr = wordstr[:j] + '\n' + wordstr[j:]
                length += 1  # 现在总长应该+1
            i = j
        i += 1
    # print(wordstr)
    return wordstr

#  分单元整理成单词文件二
def dealWord2(wordstr):
    wordstrlist = wordstr.split('\n')
    # print(wordstrlist)
    wordlist = []
    wordBook = ''
    for word in wordstrlist:
        if word == None:
            continue
        if word.find('Unit') != -1:
            wordBook += word + '\n'
        length = len(word)
        for i in range(length - 1):
            if word[i] == '（' or word[i] == '：' or word[i] == ':' or ('\u4e00' <= word[i+1] <= '\u9fff'):
                tmepword = word[:i] # 获取到单词
                # 去除首位空格
                for j1 in range(len(tmepword)):
                    if tmepword[j1] != ' ':
                        break
                for j3 in range(len(tmepword) - 1,0,-1):
                    if tmepword[j3] != ' ':
                        break
                # wordBook += word[:i] + '\n'
                wordBook += word[j1:j3+1] + '\n'
                break
    return wordBook

if __name__ == '__main__':
    # 整理一
    # wordstr = getWordStr('word/five2_1.txt')
    # wds = dealWord(wordstr)
    # writeWordStr('word/five2_2.txt', wds)
    # print('-----five2_2整理好----finish!-----')

    # 整理二
    wordstr = getWordStr('word/five2_2.txt')
    wordbook = dealWord2(wordstr)
    # wirte(wordbook, '')
    wordlist = wordbook.split('\n')
    # print(wordlist)
    unit = 0
    filename = ''
    content = ''
    pathname = 'word/人教版五年级上册英语单词/'
    for word in wordlist:
        if word == None:
            continue
        if word.find('Unit') != -1:
            if filename != '':
                content = content[:-1] # 去掉最后一空行
                writeWordStr(pathname + filename, content)
                content = ''
            unit += 1
            filename = 'unit' + str(unit) + '.txt'
            continue
        if word != None:
            content += word + '\n'
    # print(content, '-----')
    content = content[:-1] # 去掉最后一空行
    # print(content)
    if content != '':
        writeWordStr(pathname + filename, content)
    print('======finish！======')